package io.github.codelifeleno.jdbrelease.spring.boot;

import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;
import java.io.IOException;
import java.net.URISyntaxException;

@AutoConfiguration
@AutoConfigureAfter({DataSourceAutoConfiguration.class})
@EnableConfigurationProperties(AutoProperties.class)
@ConditionalOnSingleCandidate(DataSource.class)
@ConditionalOnClass(AutoService.class)
public class AutoConfig {


    @Bean
    @ConditionalOnMissingBean // 当用户未自定义该 Bean 时生效
    public AutoService autoService(AutoProperties properties, DataSource dataSource) throws IOException, URISyntaxException {
        AutoService autoService = new AutoService(properties);
        autoService.execute(dataSource);
        return autoService;
    }
}
